/**
 * @author ypz
 * @date 2021/10/26 10:32
 */
public class ThreadLocalDemo {
    private static final ThreadLocal<Integer> THREAD_LOCAL_NUM
            = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            new Thread(){
                @Override
                public void run() {
                    add();
                }
            }.start();
        }
    }
    public static void add(){
        for (int i = 0; i < 5; i++) {
            Integer num = THREAD_LOCAL_NUM.get();
            num += 1;
            THREAD_LOCAL_NUM.set(num);
            System.out.println(Thread.currentThread().getName()+ "ThreadLocal num= "+num);
        }
    }

}
